package cn.gmcx.lazy.izze.service.impl;

import cn.gmcx.lazy.izze.enums.ConcernEnum;
import cn.gmcx.lazy.izze.exception.BizException;
import cn.gmcx.lazy.izze.mapper.UserMapper;
import cn.gmcx.lazy.izze.pojo.Fans;
import cn.gmcx.lazy.izze.mapper.FansMapper;
import cn.gmcx.lazy.izze.pojo.User;
import cn.gmcx.lazy.izze.service.IFansService;
import cn.gmcx.lazy.izze.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author GMCX
 * @since 2024-07-02
 */
@Service
public class FansServiceImpl extends ServiceImpl<FansMapper, Fans> implements IFansService {

    @Autowired
    private FansMapper fansMapper;
    @Autowired
    private UserMapper userMapper;

//    @Override
//    public void updateFansNum(Integer master_id, Integer followId) {
//        QueryWrapper<Fans> wrapper = new QueryWrapper<>();
//        wrapper.eq("follower_id", followId);
//        long count = fansMapper.selectCount(wrapper);
//        User user = new User();
//        user.setId(followId);
//        user.setConcern((int) count);
//        userMapper.updateById(user);
//
//        updateFansNum2(master_id);
//
//    }
//
//    @Override
//    public void updateFansNum2(Integer master_id) {
//        QueryWrapper<Fans> wrapper = new QueryWrapper<>();
//        wrapper.eq("followee_id", master_id);
//        long count = fansMapper.selectCount(wrapper);
//        User user = new User();
//        user.setId(master_id);
//        user.setFans((int) count);
//        userMapper.updateById(user);
//    }

    @Override
    public List<User> getFriendList(Integer masterId) {
        List<User> userList = new ArrayList<>();
        QueryWrapper<Fans> wrapper = new QueryWrapper<>();
        wrapper.eq("follower_id", masterId);

        List<Fans> friendList = fansMapper.selectList(wrapper);
        friendList.forEach(item -> {
            Integer friendId = item.getFolloweeId();
            User user = userMapper.selectById(friendId);
            if (user != null) {
                user.setPassword(null);
                if (!Objects.equals(user.getId(), masterId)) {
                    userList.add(user);
                }
            }
        });
        return userList;

    }

    @Override
    @Transactional
    public void saveFans(Fans master) {
        Integer masterId = master.getFollowerId();
        Integer followId = master.getFolloweeId();
        User masterUser = new User();
        User followUser = new User();

        int insert = fansMapper.insert(master);
        if (insert == 0){
            throw new BizException("添加好友时出错");
        }

        //查询master关注数并更新
        QueryWrapper<Fans> fansQueryWrapper = new QueryWrapper<>();
        fansQueryWrapper.eq("follower_id",masterId);
        Long concernCount = fansMapper.selectCount(fansQueryWrapper);
        masterUser.setId(masterId);
        masterUser.setConcern(Math.toIntExact(concernCount));
        userMapper.updateById(masterUser);
        //-----------------

        //查询follow粉丝数并更新
        QueryWrapper<Fans> fansQueryWrapper2 = new QueryWrapper<>();
        fansQueryWrapper2.eq("followee_id",followId);
        Long fansCount = fansMapper.selectCount(fansQueryWrapper2);
        followUser.setId(followId);
        followUser.setFans(Math.toIntExact(fansCount));
        userMapper.updateById(followUser);
        //-----------------


    }

    @Override
    public ConcernEnum isConcern(Integer friendId, Integer myId) {
        if(Objects.equals(friendId, myId)){
            return ConcernEnum.MYSELF;
        }
        QueryWrapper<Fans> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follower_id", myId);
        queryWrapper.eq("followee_id", friendId);
        long isConcern = fansMapper.selectCount(queryWrapper);

        QueryWrapper<Fans> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("follower_id", friendId);
        queryWrapper2.eq("followee_id", myId);

        long isConcernToo = fansMapper.selectCount(queryWrapper2);
        if (isConcern > 0 && isConcernToo > 0) {
           return ConcernEnum.FOLLOWED_TOO;
        }
        if (isConcern >0 && isConcernToo == 0) {
            return ConcernEnum.CONCERN;
        }
        if (isConcern==0 && isConcernToo > 0) {
            return ConcernEnum.BE_FOLLOWED;
        }
        if (isConcern==0 && isConcernToo == 0) {
            return ConcernEnum.NO_FOLLOWED;
        }
        return null;
    }
}
